package com.example.day04_fragment;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class ThreeFragment extends Fragment {
    //接收传值
    public static Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 200) {
                fg3Tv.setText(msg.obj.toString());
            }
        }
    };

    private static TextView fg3Tv;
    private Button single;
    private Button mutli;
    private Button date;
    private Button time;
    private Button progress;
    Timer timer;
    int index = 0;//定义单选的全局变量

    public ThreeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_three, container, false);
        initView(view);

        single.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] items = new String[]{"男","女"};
                new AlertDialog.Builder(getActivity())
                        .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                index = which;
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getActivity(), "选中的是"+items[index], Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
            }
        });
        mutli.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] items = new String[]{"男","女"};
                boolean[] flags = new boolean[]{true,false};
                new AlertDialog.Builder(getActivity())
                        .setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                flags[which] = isChecked;
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String s = "";
                                for (int i = 0; i < flags.length; i++) {
                                    if(flags[i]){
                                        s+=items[i]+" ";
                                    }
                                }
                                Toast.makeText(getActivity(), "选中的是"+s, Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
            }
        });
        date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar  = Calendar.getInstance();
                new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        Toast.makeText(getActivity(), "选中的是"+year+"-"+(month+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();

                    }
                },1990,1,1).show();
            }
        });
        time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        Toast.makeText(getActivity(), "选中的是"+hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
                    }
                },0,0,false).show();
            }
        });

        progress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressDialog dialog = new ProgressDialog(getActivity());
                dialog.setMax(100);
                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                dialog.show();
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    int index = 0;
                    @Override
                    public void run() {
                        if(index==100){
                            timer.cancel();
                            dialog.dismiss();
                        }
                        dialog.setProgress(index+=20);
                    }
                },0,1000);

            }
        });


        return view;
    }

    private void initView(View view) {
        fg3Tv = (TextView) view.findViewById(R.id.fg3_tv);
        single = (Button) view.findViewById(R.id.single);
        mutli = (Button) view.findViewById(R.id.mutli);
        date = (Button) view.findViewById(R.id.date);
        time = (Button) view.findViewById(R.id.time);
        progress = (Button) view.findViewById(R.id.progress);
    }
}